﻿using System;
using Communication.Message.Buffer;

namespace Communication.Message.Osc
{
    // Token: 0x02000020 RID: 32
    public abstract class OscChannelMessage : AbstractChannelMessage
    {
       /// <summary>
       /// 功能码
       /// </summary>
        public byte Fun
        {
            get
            {
                return this.fun;
            }
        }

        public OscErrorCode ErrorCode
        {
            get
            {
                return this.errorCode;
            }
        }

        protected OscChannelMessage(byte fun) : base(ChannelMessageType.Client)
        {
            this.fun = fun;
            this.sendBuffer = null;
            this.receiveBuffer = null;
            this.errorCode = OscErrorCode.UNKNOWN;
        }

        public override int GetSendData(byte[] buffer, int offset)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException();
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new IndexOutOfRangeException();
            }
            return this.sendBuffer.ReadBytes(buffer, offset);
        }

        public override int FillReceivedData(byte[] receivedDatas, int offset, int length)
        {
            if (receivedDatas == null)
            {
                throw new ArgumentNullException();
            }
            if (offset < 0 || offset + length > receivedDatas.Length)
            {
                throw new IndexOutOfRangeException();
            }
            this.receiveBuffer.WriteBytes(receivedDatas, offset, length);
            this.DealwithReceiveMsg();
            return length;
        }

        protected abstract bool DealwithReceiveMsg();

        public string HexDumpSendMessage()
        {
            return MessageBuffers.HexDump(this.sendBuffer, 0, this.sendBuffer.WriterIndex());
        }

        /// <summary>
        /// bytes转换hex(接受)
        /// </summary>
        /// <returns></returns>
        public string HexDumpReceiveMessage()
        {
            return MessageBuffers.HexDump(this.receiveBuffer);
        }

        /// <summary>
        ///
        /// </summary>
        protected readonly byte fun;

        

        /// <summary>
        /// 发送集合
        /// </summary>
        protected MessageBuffer sendBuffer;
        /// <summary>
        /// 接受集合
        /// </summary>

        protected MessageBuffer receiveBuffer;
        /// <summary>
        /// 错误码
        /// </summary>

        protected OscErrorCode errorCode;
    }
}
